package org.btik.light.mobile.common.handler;

import android.content.Context;
import android.widget.Toast;
import org.btik.context.SimpleContext;

import org.btik.light.meta.event.cmd.DefaultCmd;
import org.btik.light.mobile.R;
import org.btik.light.mobile.meta.mo.DefaultDevMo;
import org.btik.light.mobile.server.MsgCenter;
import org.btik.light.tool.type.StringUtil;

import static org.btik.light.mobile.platform.conf.ContextGlobalNames.LAST_CHECKED_DEV;
import static org.btik.light.mobile.platform.conf.ContextGlobalNames.MSG_CENTER;

public abstract class CmdSender {
    protected static String choseDev;

    MsgCenter msgCenter;

    public static void onDevChecked(DefaultDevMo defaultDevMo) {
        SimpleContext.set(LAST_CHECKED_DEV, StringUtil.safeValue(choseDev));
        choseDev = defaultDevMo.getSn();
    }

    /***
     *每次发送命令时自动回填设备序列号
     * */
    protected void send(Context context, DefaultCmd command) {
        if (msgCenter == null) {
            msgCenter = SimpleContext.get(MSG_CENTER);
        }
        if (msgCenter == null) {
            Toast.makeText(context, R.string.try_again_later, Toast.LENGTH_LONG).show();
            return;
        }
        if (choseDev == null) {
            Toast.makeText(context, R.string.no_device_selected, Toast.LENGTH_LONG).show();
            return;
        }
        msgCenter.sendCmd(command);
    }
}
